1
00:00:01,210 --> 00:00:07,210
Today I'm going to introduce the if then statement or more generally known as conditional statements.

2
00:00:07,210 --> 00:00:12,120
So something has to be true in order to execute a block of code.

3
00:00:12,130 --> 00:00:13,870
I have a flat terrain here.

4
00:00:13,870 --> 00:00:15,340
There's nothing in this world.

5
00:00:15,340 --> 00:00:16,990
It's just going to be a demo.

6
00:00:17,110 --> 00:00:19,390
So we'll go ahead and get a part.

7
00:00:19,390 --> 00:00:21,400
I want the block, right?

8
00:00:21,460 --> 00:00:26,230
So get a block and let's go to this block.

9
00:00:26,590 --> 00:00:28,870
Take a look at the size.

10
00:00:28,990 --> 00:00:30,280
Open up size.

11
00:00:30,280 --> 00:00:34,690
It's four one and two on the X, Y and Z respectively.

12
00:00:34,690 --> 00:00:36,160
We'll go ahead and remember that.

13
00:00:36,280 --> 00:00:38,470
Let's add a script to our part.

14
00:00:38,500 --> 00:00:45,310
Hit the plus sign script and then I'm going to call this if then because we are going to learn about

15
00:00:45,310 --> 00:00:51,970
if Ben's in this script to get rid of this print statement and I'm going to get a variable for my part.

16
00:00:52,870 --> 00:00:58,060
So I'll do script dot parent to reference the part and put it in the part variable.

17
00:00:58,450 --> 00:01:01,540
I'm also going to get two more variables.

18
00:01:01,810 --> 00:01:05,560
I want to get one for the x size of the part.

19
00:01:05,560 --> 00:01:09,220
So I'll say part dot size dot x.

20
00:01:09,220 --> 00:01:16,660
Now in order to set the X, Y and z the size, you need a vector three but you can read the individual

21
00:01:17,090 --> 00:01:18,310
X, Y and Z.

22
00:01:18,610 --> 00:01:20,710
We're just going to use X and Y.

23
00:01:21,250 --> 00:01:21,450
All right.

24
00:01:21,460 --> 00:01:24,730
So say part size, dot y.

25
00:01:25,360 --> 00:01:26,140
Cool.

26
00:01:26,170 --> 00:01:32,980
Now I want to execute code if my X is four studs, which it is.

27
00:01:32,980 --> 00:01:33,310
Right.

28
00:01:33,310 --> 00:01:45,100
So I'm going to do this if statement if I want to do x equals E equals or then print x was the right

29
00:01:45,100 --> 00:01:45,880
size.

30
00:01:45,880 --> 00:01:51,220
X was the right size.

31
00:01:52,030 --> 00:01:52,690
Cool.

32
00:01:52,720 --> 00:01:55,930
Notice that this is equals equals.

33
00:01:55,930 --> 00:02:01,000
That is for comparison double equals single equals is for assignment.

34
00:02:01,000 --> 00:02:06,910
We're putting this value into that variable here is here is comparison.

35
00:02:07,450 --> 00:02:09,580
All right, now I said block a code.

36
00:02:09,580 --> 00:02:13,270
This is a single statement so we can make it a block.

37
00:02:13,300 --> 00:02:15,640
Let's just put more than one print statement in there.

38
00:02:15,670 --> 00:02:20,050
Maybe I'll do this little separator here.

39
00:02:20,050 --> 00:02:21,760
So that looks like a block of code.

40
00:02:21,760 --> 00:02:23,830
You've got three print statements going.

41
00:02:24,770 --> 00:02:25,460
Cool.

42
00:02:25,490 --> 00:02:29,180
You also might notice that we have indents here.

43
00:02:29,180 --> 00:02:30,860
We often indent.

44
00:02:31,640 --> 00:02:37,280
So you have a visual, a visual cue of where the block starts and where the block ends.

45
00:02:37,310 --> 00:02:41,840
Now, in Lua you don't have to have the indent, correct.

46
00:02:41,990 --> 00:02:43,850
In Python you do.

47
00:02:43,880 --> 00:02:49,660
This would ere the actual indents designate the beginning and end of the block.

48
00:02:49,670 --> 00:02:50,870
But we could run this.

49
00:02:50,870 --> 00:02:51,770
No problem.

50
00:02:52,340 --> 00:02:53,730
I don't like that, though.

51
00:02:53,750 --> 00:02:56,920
I'm going to show you a little trick on this script tab.

52
00:02:56,930 --> 00:03:01,970
Go to this format section or selection format document.

53
00:03:02,330 --> 00:03:03,200
Boom.

54
00:03:03,230 --> 00:03:05,840
And that way, you know how to format the document.

55
00:03:05,840 --> 00:03:06,890
It'll do it for you.

56
00:03:06,890 --> 00:03:11,900
If you get a messed up, you should check to see if make sure that some of your ends are in the right

57
00:03:11,900 --> 00:03:13,640
place or you don't have an error.

58
00:03:14,210 --> 00:03:17,540
Let's go and run this and see if X is in fact the right size.

59
00:03:17,750 --> 00:03:20,360
I'm going to go to view output.

60
00:03:20,360 --> 00:03:27,380
I got my output window here, maybe make it a little bigger home play and it should print right away.

61
00:03:27,380 --> 00:03:28,130
And it did.

62
00:03:28,130 --> 00:03:29,570
X is the right size.

63
00:03:29,570 --> 00:03:30,470
Well, we knew that.

64
00:03:30,470 --> 00:03:31,490
We knew it was four.

65
00:03:32,420 --> 00:03:32,840
All right.

66
00:03:32,840 --> 00:03:34,400
Let's just add a little bit to this.

67
00:03:34,400 --> 00:03:40,370
This is not going to be an exhaustive talk on if statements just want to introduce enough so that we

68
00:03:40,370 --> 00:03:42,800
can add to it in our other videos.

69
00:03:42,890 --> 00:03:48,770
So we need this that to be true in order for it to be executed.

70
00:03:49,280 --> 00:03:52,250
In order for the block to be executed, you could write true.

71
00:03:52,250 --> 00:03:56,900
But that's not very interesting because the word true is always going to be true.

72
00:03:57,200 --> 00:03:57,870
All right.

73
00:03:57,890 --> 00:03:59,840
Another thing, control Z.

74
00:03:59,870 --> 00:04:01,130
Control Z.

75
00:04:01,820 --> 00:04:02,750
There we go.

76
00:04:03,020 --> 00:04:08,420
You can you can add more than one thing to test in the overall condition.

77
00:04:08,420 --> 00:04:17,000
So I'll say if X equals equals four and and Y equals equals one.

78
00:04:17,210 --> 00:04:17,750
Right.

79
00:04:17,750 --> 00:04:19,220
And Y is one.

80
00:04:19,820 --> 00:04:21,230
We can check on our size.

81
00:04:21,230 --> 00:04:22,730
But I remember that it was.

82
00:04:22,730 --> 00:04:30,560
So if I have an and here this has to be correct and this has to be correct, right.

83
00:04:30,560 --> 00:04:33,290
So that the overall thing will be true.

84
00:04:33,950 --> 00:04:38,780
Let's say X and Y was the right size.

85
00:04:39,960 --> 00:04:40,740
I won't bother.

86
00:04:40,890 --> 00:04:41,100
Yeah.

87
00:04:41,100 --> 00:04:42,120
Let's make these little bigger.

88
00:04:45,990 --> 00:04:47,510
That IntelliSense gets in the way.

89
00:04:47,520 --> 00:04:48,720
This is the IntelliSense.

90
00:04:48,720 --> 00:04:52,260
It helps you out, but sometimes it gets in your way when you're doing videos.

91
00:04:52,290 --> 00:04:53,100
There we go.

92
00:04:53,280 --> 00:04:53,820
All right.

93
00:04:53,820 --> 00:04:56,040
So let's go ahead and see how this works.

94
00:04:56,070 --> 00:05:00,330
It should print out and it does.

95
00:05:00,330 --> 00:05:01,500
Pretty cool.

96
00:05:03,210 --> 00:05:04,000
All right.

97
00:05:04,020 --> 00:05:05,790
I'm not going to get into negation.

98
00:05:06,120 --> 00:05:07,980
Let's go ahead and do an or though.

99
00:05:07,980 --> 00:05:13,950
What does that mean or means if this is right or this is right.

100
00:05:13,950 --> 00:05:15,940
So we only need one to be right.

101
00:05:15,960 --> 00:05:19,470
Let's say X or Y was the right size.

102
00:05:20,010 --> 00:05:21,240
I'll make that a little bigger.

103
00:05:22,930 --> 00:05:25,150
That's not really not that's not really helping, is it?

104
00:05:25,410 --> 00:05:28,810
Okay, so let's go to change.

105
00:05:29,710 --> 00:05:34,150
X to three making that incorrect.

106
00:05:34,150 --> 00:05:37,660
So if I have an ore statement, this is going to be checked.

107
00:05:37,660 --> 00:05:40,540
If it's false, we're going to check this.

108
00:05:40,540 --> 00:05:46,180
If either one of them is true, the overall thing will be true because we're doing it or so this will

109
00:05:46,180 --> 00:05:47,020
still work.

110
00:05:47,020 --> 00:05:49,300
It's an ore and would not.

111
00:05:50,500 --> 00:05:51,640
So if we do, let's try.

112
00:05:51,640 --> 00:05:52,140
And.

113
00:05:52,480 --> 00:05:53,320
And.

114
00:05:57,910 --> 00:05:59,830
X and Y.

115
00:05:59,860 --> 00:06:01,150
This is not going to work.

116
00:06:01,150 --> 00:06:02,480
We're not going to get the print out.

117
00:06:02,500 --> 00:06:04,030
We did not.

118
00:06:04,450 --> 00:06:05,720
But all will work.

119
00:06:05,740 --> 00:06:07,090
You can trust me on that one.

120
00:06:07,870 --> 00:06:08,490
All right.

121
00:06:08,500 --> 00:06:10,240
I want to show you something else.

122
00:06:10,450 --> 00:06:17,290
If that if statement fails, you can have something called an LL statement, and we'll just say.

123
00:06:18,490 --> 00:06:19,180
Print.

124
00:06:20,620 --> 00:06:23,170
The numbers were not correct.

125
00:06:23,710 --> 00:06:30,580
The numbers were not correct.

126
00:06:30,730 --> 00:06:32,630
Now, this is going to print, right?

127
00:06:32,680 --> 00:06:33,490
We're going to go in here.

128
00:06:33,490 --> 00:06:36,640
We're going to see that that doesn't work because our X is wrong.

129
00:06:36,640 --> 00:06:39,460
And then this is going to print out, let's do it.

130
00:06:42,060 --> 00:06:44,280
The numbers were not correct.

131
00:06:45,000 --> 00:06:45,900
All right.

132
00:06:45,900 --> 00:06:47,790
And I want to show you something else, too.

133
00:06:48,420 --> 00:06:50,250
For now, I'm just going to move this down.

134
00:06:51,240 --> 00:07:00,120
Maybe we want to check to see if this is failing or that's failing in our statement so I can do something

135
00:07:00,120 --> 00:07:01,760
called LCF.

136
00:07:01,770 --> 00:07:03,150
So no space.

137
00:07:03,150 --> 00:07:08,870
If you put a space in there, you're essentially putting another block of if inside an else.

138
00:07:09,450 --> 00:07:14,670
So we'll say elseif x equals equals three.

139
00:07:14,670 --> 00:07:18,000
Then print x was right.

140
00:07:21,840 --> 00:07:24,690
I will say lcf.

141
00:07:25,290 --> 00:07:27,600
Y equals equals one.

142
00:07:27,720 --> 00:07:31,950
Then print y was right.

143
00:07:33,210 --> 00:07:38,360
Now the first correct condition we get, we're going to execute the block.

144
00:07:38,370 --> 00:07:41,290
Then we're going to leave this entire compound statement.

145
00:07:41,310 --> 00:07:43,200
Only one of these is going to fire.

146
00:07:43,380 --> 00:07:45,560
So this is wrong.

147
00:07:45,570 --> 00:07:46,710
We're going to come down here.

148
00:07:46,710 --> 00:07:49,530
We're going to find out this is wrong because X is actually four.

149
00:07:49,560 --> 00:07:54,690
We're going to see that this is true and we're going to see y was right.

150
00:07:55,500 --> 00:07:56,710
And it's got in print.

151
00:07:58,850 --> 00:08:00,740
Why was right.

152
00:08:00,920 --> 00:08:01,900
All righty.

153
00:08:03,240 --> 00:08:06,420
So if this one what if we have this?

154
00:08:06,930 --> 00:08:08,450
This is correct.

155
00:08:08,460 --> 00:08:09,750
Let's make this correct.

156
00:08:10,200 --> 00:08:11,280
Let's make them all correct.

157
00:08:11,280 --> 00:08:11,730
Right.

158
00:08:11,730 --> 00:08:13,800
So this is this is going to be true.

159
00:08:14,670 --> 00:08:18,240
This is going to be true and this is going to be true.

160
00:08:18,510 --> 00:08:22,560
What's going to print out while it's going to be the first one?

161
00:08:23,250 --> 00:08:27,420
These will not be tested and the else, of course, will be skipped over.

162
00:08:27,720 --> 00:08:28,980
Let's go ahead and try it.

163
00:08:29,320 --> 00:08:32,580
A few output make that bigger.

164
00:08:33,770 --> 00:08:35,210
And I had to play from here.

165
00:08:36,270 --> 00:08:38,560
Look at that, the first one.

166
00:08:38,580 --> 00:08:42,690
Now, if you take a computer science class, this is going to get really, really in-depth.

167
00:08:42,690 --> 00:08:46,270
They're going to do something called truth tables, the Morgan's Laws.

168
00:08:46,290 --> 00:08:48,680
These are going to get big and scary.

169
00:08:48,690 --> 00:08:52,440
You're going to have stuff like the counter positive and all this other stuff here.

170
00:08:52,440 --> 00:08:55,530
We just want to know enough to get things working.

171
00:08:55,530 --> 00:09:00,960
And I want you to be familiar enough so that when you see it in the code in the preceding videos, you're

172
00:09:00,960 --> 00:09:03,210
not freaked out saying, Oh man, what's that?

173
00:09:03,210 --> 00:09:06,230
So good luck with the if statement.

174
00:09:06,240 --> 00:09:11,370
It's a valuable tool and we'll do something a little more interesting with it coming up in the next

175
00:09:11,370 --> 00:09:12,270
videos.